package com.infogant.web;

/**
 * Created by peter.henell on 19/02/15.
 */

import com.infogant.dao.core.AbstractPersonDAO;
import com.infogant.dao.core.PersonDAO;
//import com.infogant.web.resouces.PersonResource;
import com.infogant.web.resouces.AddUserResource;
import com.infogant.web.resouces.HelloWorldResource;
import io.dropwizard.Application;
import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.skife.jdbi.v2.DBI;

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
    public static void main(String[] args) throws Exception {
        new HelloWorldApplication().run(args);
    }

    @Override
    public String getName() {
        return "hello-world";
    }

    @Override
    public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
        // nothing to do yet
    }

    @Override
    public void run(HelloWorldConfiguration configuration,
                    Environment environment) throws ClassNotFoundException {

        final HelloWorldResource helloWorldResource = new HelloWorldResource(
                configuration.getTemplate(),
                configuration.getDefaultName()
        );
        environment.jersey().register(helloWorldResource);

        final TemplateHealthCheck healthCheck =
                new TemplateHealthCheck(configuration.getTemplate());
        environment.healthChecks().register("template", healthCheck);

        final DBIFactory factory = new DBIFactory();
        final DBI jdbi = factory.build(environment, configuration.getDataSourceFactory(), "postgresql");
        final AbstractPersonDAO personDAO = jdbi.onDemand(AbstractPersonDAO.class);

//        environment.jersey().register(new PersonResource(personDAO));

        final AddUserResource addUserResource = new AddUserResource(
                configuration.getTemplate(),
                configuration.getDefaultName(),
                personDAO
        );
        environment.jersey().register(addUserResource);


    }

}